$(document).ready(function() {

$("#map_loc_mc_id").select2();    
    var geocoder;
    var map;
    var defaultLat = '';
    var defaultLon = '';
    if (LATITUDE === '') {
        defaultLat = 37.98;
    } else {
        defaultLat = LATITUDE;
    }
    if (LONGITUDE === '') {
        defaultLon = 23.73;
    } else {
        defaultLon = LONGITUDE;
    }
    geocoder = new google.maps.Geocoder();


    var latlng = new google.maps.LatLng(defaultLat, defaultLon);
	
	if(MAP_DEFAULT_LAYOUT == 'SATELLITE'){
            var typeId = google.maps.MapTypeId.SATELLITE;
	} else {
            var typeId = google.maps.MapTypeId.ROADMAP;
    }

    if (ISKMLPRESENT === 'YES') {
        var mapOptions = {
            zoom: 25,
            center: latlng,
			 mapTypeId: typeId
            //mapTypeId: google.maps.MapTypeId.SATELLITE
        }
    } else {
        var mapOptions = {
            zoom: 10,
            center: latlng,
			 mapTypeId: typeId
            //mapTypeId: google.maps.MapTypeId.SATELLITE
        }
    }

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    if (ISKMLPRESENT === 'YES') {
        var bermudaTriangle;
        var triangleCoords = new Array();

        var markerArray = JSON.parse(MARKERS);

        $.each(markerArray, function(index, value) {
            var latitudelongitude = new google.maps.LatLng(value.lat, value.lon);
            triangleCoords.push(latitudelongitude);
        });


        // Construct the polygon.
        bermudaTriangle = new google.maps.Polygon({
            paths: triangleCoords,
            strokeColor: '#FF0000',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: '#FF0000',
            fillOpacity: 0.35
        });

        if (!google.maps.Polygon.prototype.getBounds) {

            google.maps.Polygon.prototype.getBounds = function() {
                var bounds = new google.maps.LatLngBounds()
                this.getPath().forEach(function(element, index) {
                    bounds.extend(element)
                })
                return bounds
            }

        }

        var polygonlatlng = new google.maps.LatLng(bermudaTriangle.getBounds().getCenter().lat(), bermudaTriangle.getBounds().getCenter().lng());
        map.setCenter(polygonlatlng);
        bermudaTriangle.setMap(map);

        marker = new google.maps.Marker({
            map: map,
            draggable: true,
            animation: google.maps.Animation.DROP,
            position: polygonlatlng
        });



    } else {

        marker = new google.maps.Marker({
            map: map,
            draggable: true,
            animation: google.maps.Animation.DROP,
            position: latlng
        });


    }

    google.maps.event.addListener(marker, 'dragend', function(event) {

        latitude = event.latLng.lat();
        longitude = event.latLng.lng();

        jQuery('#map_loc_lat').val(latitude);
        jQuery('#map_loc_long').val(longitude);

        var draglatlng = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
        geocoder.geocode({'latLng': draglatlng}, function(data, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var add = data[1].formatted_address; //this is the full address
                jQuery('#map_loc_address').val(add);
            } else {
                alert('Geocode was not successful for finding address for following reason: ' + status);
            }
        });
    });

    $('#current-location').click(function() {

        // Try HTML5 geolocation
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var pos = new google.maps.LatLng(position.coords.latitude,
                        position.coords.longitude);

                geocoder.geocode({'latLng': pos}, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        if (results[1]) {
                            jQuery('#map_loc_address').val(results[1].formatted_address);
                        }
                    } else {
                        //alert("Geocoder failed due to: " + status);
                    }
                });


                jQuery('#map_loc_lat').val(position.coords.latitude);
                jQuery('#map_loc_long').val(position.coords.longitude);

                map.setCenter(pos);
                marker.setPosition(pos);

                //map.setCenter(pos);
            });
        } else {
            alert(" Browser doesn't support Geolocation");
            // Browser doesn't support Geolocation
            //handleNoGeolocation(false);
        }

    });


    $('#search-map').click(function() {

        var address = document.getElementById('map_loc_address').value;
        geocoder.geocode({'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {

                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();

                jQuery('#map_loc_lat').val(latitude);
                jQuery('#map_loc_long').val(longitude);

                map.setCenter(results[0].geometry.location);
                marker.setPosition(results[0].geometry.location);
                /*
                 var marker = new google.maps.Marker({
                 map: map,
                 position: results[0].geometry.location
                 });
                 */
            } else {
                alert('Geocode was not successful for the following reason: ' + status);

            }
        });
    });



    $('#rootwizard').bootstrapWizard({
        'tabClass': 'form-wizard',
        'onTabShow': function(tab, navigation, index) {



            //Scroll Logic
            $("html, body").animate({
                scrollTop: 0
            }, "slow");


            $('#map-canvas').css({height: 500, width: 970});

            x = map.getZoom();
            c = map.getCenter();
            google.maps.event.trigger(map, 'resize');
            map.setZoom(x);
            map.setCenter(c);

            $('.submitfrmbtn').removeClass('display-none');
            $('.lastbutton').removeClass('display-none');
            if (index === 2) {
                $('.submitfrmbtn').removeClass('display-none');
                $('.lastbutton').addClass('display-none');
            } else {
                $('.submitfrmbtn').addClass('display-none');
                $('.lastbutton').removeClass('display-none');
            }

        },
        'onTabClick': function(tab, navigation, index) {
        },
        'onNext': function(tab, navigation, index) {

            $('.submitfrmbtn').removeClass('display-none');
            $('.lastbutton').removeClass('display-none');

            if (index === 2) {
                $('.submitfrmbtn').removeClass('display-none');
                $('.lastbutton').addClass('display-none');
            } else {
                $('.submitfrmbtn').addClass('display-none');
                $('.lastbutton').removeClass('display-none');
            }
        },
        'onPrevious': function(tab, navigation, index) {

            if (index === 2) {
                $('.submitfrmbtn').removeClass('display-none');
                $('.lastbutton').addClass('display-none');
            } else {
                $('.submitfrmbtn').addClass('display-none');
                $('.lastbutton').removeClass('display-none');
            }
        }
    });

});
	 